package com.tanhua.sso.controller;


import cn.hutool.core.convert.Convert;
import com.tanhua.sso.service.UserInfoService;
import com.tanhua.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UsersController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存头像
     *
     * @param headPhoto
     * @return
     */
    @PostMapping("header")
    public Object saveAvatar(@RequestHeader("Authorization") String token,
                           @RequestParam("headPhoto") MultipartFile headPhoto) {
        return this.userInfoService.userLogo(token, headPhoto);
    }

    /**
     * 修改手机号- 1 发送短信验证码
     *
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity<Void> sendVerificationCode(@RequestHeader("Authorization") String token) {
        try {
            Object flag = this.userService.sendVerificationCode(token);
            if (flag != null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 修改手机号- 2 校验验证码
     *
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity<Map<String, Object>> checkVerificationCode(@RequestBody Map<String, Object> param,
                                                      @RequestHeader("Authorization") String token) {
        try {
            Object code = param.get("verificationCode");
            Boolean flag = this.userService.checkVerificationCode(token, code);
            if (flag != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("verification", flag );
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 保存手机号
     * @param param
     * @param token
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity<Void> updatePhone(@RequestBody Map<String, Object> param,
                                            @RequestHeader("Authorization") String token) {
        try {
            String phone = Convert.toStr(param.get("phone"));
            Object obj = this.userService.updatePhone(phone, token);
            if (obj != null) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
